package com.aigou.admin.aigouspringboot.controller.admin;


import com.aigou.admin.aigouspringboot.result.Result;
import com.aigou.admin.aigouspringboot.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;

/**
 * @Author Mr.LiFuXing
 * @Date 2024/11/28 10:06
 * @Title CommonController
 * @Package com.sky.controller.admin
 */

@RestController
@RequestMapping
@Slf4j
public class CommonController {

    @Resource
    private AliOssUtil aliOssUtil;


    @PostMapping("/upload")
    public Result upload(MultipartFile file) {

        // 获取文件全名   12345.png
        String originalFilename = file.getOriginalFilename();
        // 截取
        assert originalFilename != null;
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        try {
            log.info("文件上传{}", file);
            String filePath = aliOssUtil.upload(file.getBytes(), UUID.randomUUID() + substring);
            return Result.success(filePath);
        } catch (IOException e) {
            log.info("文件上传失败{}", e.getMessage());
        }

        return Result.error("上传失败");
    }
}
